﻿namespace SharpSoft.Data.GSQL
{
    using Expressions;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// 创建表语句
    /// </summary>
    public class CreateTableStatement : IStatement
    {
        /// <summary>
        /// 决定创建表之前检查表是否不存在
        /// </summary>
        public bool IfNotExists { get; set; }
        /// <summary>
        /// 表名称
        /// </summary>
        public VariableExpression Table { get; set; }
        /// <summary>
        /// 表备注
        /// </summary>
        public string Comment { get; private set; }
        /// <summary>
        /// 表使用的数据引擎
        /// </summary>
        public string Engine { get; private set; }
        /// <summary>
        /// 默认文字编码
        /// </summary>
        public string DefaultCharset { get; set; }
        public CreateTableStatement()
        {
            ColumnDefines = new List<IColumnDefine>();
        }
        /// <summary>
        /// 设置表的属性
        /// </summary>
        /// <param name="dic"></param>
        internal void SetTableAttribute(Dictionary<string, IExpression> dic)
        {
            foreach (var item in dic)
            {
                switch (item.Key)
                {
                    case "COMMENT":
                        if (item.Value is StringExpression stre)
                        {
                            this.Comment = stre.Content;
                        }
                        else
                        {
                            throw new System.Exception($"创建表语句{Table}的注释参数必须为字符串。");
                        }
                        break;
                    case "ENGINE":
                        if (item.Value is VariableExpression vare1)
                        {
                            this.Engine = vare1.Name;
                        }
                        else
                        {
                            throw new System.Exception($"创建表语句{Table}的ENGINE设置无效。");
                        }
                        break;
                    case "DEFAULT CHARSET":
                        if (item.Value is VariableExpression vare2)
                        {
                            this.DefaultCharset = vare2.Name;
                        }
                        else
                        {
                            throw new System.Exception($"创建表语句{Table}的DEFAULT CHARSET设置无效。");
                        }
                        break;
                    default:
                        throw new System.Exception($"创建表语句{Table}的设置[{item.Key}]暂不支持。");
                }
            }
        }
        public List<IColumnDefine> ColumnDefines { get; private set; }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("CREATE TABLE ");
            if (IfNotExists)
            {
                sb.Append("IFNOTEXISTS ");
            }
            sb.Append(Table).Append(" (");
            foreach (var item in ColumnDefines)
            {
                sb.Append(item).Append(",");
            }

            sb.Append(")");
            if (!string.IsNullOrEmpty(Comment))
            {
                sb.Append($" COMMENT = {SqlUtility.ConvertToTSqlText(Comment)}");
            }
            return sb.ToString();
        }
    }
}
